package com.gw.yunke.web;

import com.gw.yunke.domain.Note;
import com.gw.yunke.domain.User;
import com.gw.yunke.domain.dto.ResultData;
import com.gw.yunke.service.INoteService;
import com.gw.yunke.service.IUserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/yunke/note")
public class NoteController {

  private static final Logger log = LoggerFactory.getLogger(CommentController.class);

  @Autowired
  private IUserService userService;

  @Autowired
  private INoteService noteService;

  @ApiOperation(value = "获取笔记",notes = "需登录状态 携带cookie",consumes = "application/x-www-form-urlencoded",response = ResultData.class)
  @ApiImplicitParams({
          @ApiImplicitParam(name = "content",required = true,value = "content 内容"),
          @ApiImplicitParam(name = "course_id",required = true,value = "课程id")

  })
  @RequestMapping(value = "/getNote",method = RequestMethod.POST)
  public ResultData<Note> getNote(@RequestParam(value = "course_id",required = true)Long course_id,
                                     @CookieValue("sso")String token) throws Exception {

    User user = userService.getUserInfoByToken(token);
    if(user == null){
      log.error("用户验证出错");
      throw new Exception("用户验证错误");
    }
    Note note = noteService.getNote(course_id,user.getId());
    ResultData<Note> resultData = new ResultData<>();

    resultData.setCode(ResultData.OK);
    resultData.setMessage("ok");
    resultData.setData(note);
    return resultData;
  }

  @ApiOperation(value = "存储笔记",notes = "需登录状态 携带cookie",consumes = "application/x-www-form-urlencoded",response = ResultData.class)
  @ApiImplicitParams({
          @ApiImplicitParam(name = "content",required = true,value = "content 内容"),
          @ApiImplicitParam(name = "course_id",required = true,value = "课程id")

  })
  @RequestMapping(value = "/save",method = RequestMethod.POST)
  public ResultData<String> saveNote(@RequestParam(value = "content",required = true)String content,
                                          @RequestParam(value = "course_id",required = false)Long course_id,
                                          @CookieValue("sso")String token) throws Exception {

    User user = userService.getUserInfoByToken(token);
    if(user == null){
      log.error("用户验证出错");
      throw new Exception("用户验证错误");
    }
    Note note = new Note();
    note.setUserId(user.getId());
    note.setContent(content);
    note.setCourseId(course_id);

    int result = noteService.saveNote(note);
    ResultData<String> resultData = new ResultData<>();
    if(result > 0) {
      resultData.setCode(ResultData.OK);
      resultData.setMessage("ok");
      resultData.setData("成功");
    }else {
      resultData.setCode(ResultData.ERROR);
      resultData.setMessage("failed");
      resultData.setData("失败");
    }
    return resultData;
  }


}
